﻿using MediatR;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Domains.zjy;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.zjyApplication.Commands.Menu
{
    public class SearchMenuHandler : IRequestHandler<SearchMenuCommand, Result<List<Permissions>>>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchMenuHandler> _logger;

        /// <summary>
        /// 用户仓储
        /// </summary>
        private readonly IBaseRepository<Users> _usersRepository;

        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IBaseRepository<Roles> _rolesRepository;

        /// <summary>
        /// 权限仓储
        /// </summary>
        private readonly IBaseRepository<Permissions> _permissionsRepository;

        /// <summary>
        /// 角色权限仓储
        /// </summary>
        private readonly IBaseRepository<RolePermissions> _rolePermissionsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="usersRepository"></param>
        /// <param name="rolesRepository"></param>
        /// <param name="permissionsRepository"></param>
        /// <param name="rolePermissionsRepository"></param>
        public SearchMenuHandler(ILogger<SearchMenuHandler> logger,
                                 IBaseRepository<Users> usersRepository,
                                 IBaseRepository<Roles> rolesRepository,
                                 IBaseRepository<Permissions> permissionsRepository,
                                 IBaseRepository<RolePermissions> rolePermissionsRepository)
        {
            _logger = logger;
            _usersRepository = usersRepository;
            _rolesRepository = rolesRepository;
            _permissionsRepository = permissionsRepository;
            _rolePermissionsRepository = rolePermissionsRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<Permissions>>> Handle(SearchMenuCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<Permissions>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var query = (from a in await _usersRepository.GetAll()
                             join b in await _rolesRepository.GetAll() on a.RolesId equals b.RolesId
                             join c in await _rolePermissionsRepository.GetAll() on b.RolesId equals c.RolesId
                             join d in await _permissionsRepository.GetAll() on c.PermissionsId equals d.PermissionsId
                             where (a.UserId == request.UserId)
                             select new Permissions()
                             {
                                 PermissionsId = d.PermissionsId,
                                 PermissionsFunction = d.PermissionsFunction,
                                 URL = d.URL,
                                 ParentLevelId = d.ParentLevelId,
                             }).ToList();
                if (query != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = query;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("获取菜单列表成功" + ex.Message);
            }
            return result;
        }
    }
}
